package com.lenovo.vctlclient;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class HelpFaqActivity extends Activity{
	private List<View> listView;
	private ImageView[] imageViews;
	private ViewGroup main;
	private Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		SharedPreferences share = getSharedPreferences("firstLogin", Context.MODE_PRIVATE);
		int time = share.getInt("time", 0);
		if(time == 1){
			Intent intent = new Intent();
			intent.setClass(mContext, WelcomeActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}else{
//			setContentView(layoutResID);
			
			Editor editor = share.edit();
			editor.putInt("time", 1);
			editor.commit();
			
			
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			
			LayoutInflater inflater = getLayoutInflater();
			
			listView = new ArrayList<View>();
			listView.add(inflater.inflate(R.layout.guide_item1, null));
			listView.add(inflater.inflate(R.layout.guide_item2, null));
			listView.add(inflater.inflate(R.layout.guide_item3, null));
			
			imageViews = new ImageView[listView.size()];
			main = (ViewGroup) inflater.inflate(R.layout.helpfaq, null);
			
			ViewGroup group = (ViewGroup) main.findViewById(R.id.group);
			ViewPager viewPager = (ViewPager) main.findViewById(R.id.guidePages);
			
			for(int i = 0; i< listView.size(); i++){
				ImageView img = new ImageView(mContext);
				img.setLayoutParams(new LayoutParams(20, 20));
				img.setPadding(20, 0, 20, 0);
				imageViews[i] = img;
				
				if(i == 0){
					
				}else{
					
				}
				
				group.addView(img);
				if(i == 3){
				}
			}
			
			setContentView(main);
			viewPager.setAdapter(guideAdapter);
//			viewPager.setOnPageChangeListener(listener);
			
			
		}
	}
	
	
	
	private PagerAdapter guideAdapter = new PagerAdapter() {
		
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public int getCount() {
			return listView.size();
		}
		
		
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(listView.get(position));
		};
		
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(listView.get(position));
			switch (position) {
			case 2:
				Button start = (Button) listView.get(position).findViewById(R.id.start_weaver);
				
				start.setOnClickListener(listener);
				break;

			default:
				break;
			}
			return listView.get(position);
		};
	};
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(mContext, WelcomeActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
	};
	
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		doLogout();
	}
	
	  private void doLogout() {
	        Intent next=new Intent(this,WelcomeActivity.class);
	        next.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(next);
	        finish();
	    }

}
